<?php

require_once(dirname(__FILE__) . '/../config.php');

class shoppingCart {

	var $total = 0;
	var $totalShipping = 0;
	var $itemCount = 0;
	var $itemQuantities = array();
	var $itemNames = array();
	var $itemDescriptions = array();
	var $itemIDs = array();
	var $itemPrices = array();
	var $itemFormats = array();

	var $shipping_cad = array();
	var $shipping_usd = array();
	var $shipping_int = array();
	var $shipping_each = array();

	function get_contents() {

		$items = array();
		foreach ($this->itemNames as $key => $tmp_item) {
			$item = FALSE;
			$item['id'] = $tmp_item;
			$item['qty'] = $this->itemQuantities[$key];
			$item['price'] = $this->itemPrices[$key];
			$item['info'] = $this->itemNames[$key];
			$item['format'] = $this->itemFormats[$key];
			$item['subtotal'] = $item['qty'] * $item['price'];
			$item['productid'] = $this->itemIDs[$key];
			$item['shipping_usd'] = $this->shipping_usd[$key];
			$item['shipping_cad'] = $this->shipping_cad[$key];
			$item['shipping_int'] = $this->shipping_int[$key];
			$item['shipping_each'] = $this->shipping_each[$key];
			$items[] = $item;
		}
		return $items;
	}

	function add_item($name, $price, $itemID=NULL, $quantity = 1, $shipping=NULL, $format=NULL) {
		if ($this->itemQuantities[$itemID] > 0) {
			$this->itemQuantities[$itemID] = $this->itemQuantities[$itemID]+$quantity;
		} else {
			$this->itemQuantities[$itemID] = $quantity;
			$this->itemIDs[$itemID] = $itemID;
			$this->itemPrices[$itemID] = $price;
			$this->itemNames[$itemID] = $name;
			$this->itemFormats[$itemID] = $format;
			$this->shipping_usd[$itemID] = $shipping['shipping_usd'];
			$this->shipping_cad[$itemID] = $shipping['shipping_cad'];
			$this->shipping_int[$itemID] = $shipping['shipping_int'];
			$this->shipping_each[$itemID] = $shipping['shipping_each'];
		}

		$this->update_total();
	}

	function remove_item($id) {
		unset($this->itemNames[$id]);
		unset($this->itemPrices[$id]);
		unset($this->itemQuantities[$id]);
		unset($this->itemIDs[$id]);
		$this->update_total();
	}

	function delete_item($id) {
		array_splice($this->itemNames, $id, 1);
		array_splice($this->itemPrices, $id, 1);
		array_splice($this->itemQuantities, $id, 1);
		$this->update_total();
	}

	function empty_cart() {
		$this->total = 0;
		$this->itemNames = NULL;
		$this->itemPrices = NULL;
		$this->itemQuantities = NULL;
	}

	function update_total() {
		$this->total = NULL;
		foreach ($this->itemPrices as $key => $item) {
			$this->total = $this->total + ($item * $this->itemQuantities[$key]);
		}
#		$this->update_session();
	}

	function clear_cart() {
		$this->total = 0;
		$this->totalShipping = 0;
		$this->itemCount = 0;
		$this->itemQuantities = array();
		$this->itemNames = array();
		$this->itemDescriptions = array();
		$this->itemIDs = array();
		$this->itemPrices = array();
		$this->shipping_cad = array();
		$this->shipping_usd = array();
		$this->shipping_int = array();
		$this->shipping_each = array();
	}

	# Is there shipping on any of the items in the cart?
	function is_shipping() {
		foreach ($this->shipping_cad as $shipping) {
			if ($shipping > 0) return TRUE;
		}
		foreach ($this->shipping_int as $shipping) {
			if ($shipping > 0) return TRUE;
		}
		foreach ($this->shipping_usd as $shipping) {
			if ($shipping > 0) return TRUE;
		}
		return FALSE;
	}

	function clear_session() {
		$_SESSION = NULL;
	}

	function output_pay_button_form($type) {

		if ($type == "amazon") {
			$location = WEB_ROOT . 'cart/amazon_forward.php';
			$id = 'amazon_payform';
			$image = '<input alt="Pay Now With Amazon" type="image" src="'. WEB_ROOT .'images/GLPayNowWithAmazon.png" style="border-style: hidden">';
		} else if ($type == "paypal") {
			$location = 'https://' . PAYPAL_URL . '/cgi-bin/webscr';
			$id = "payform";
			$business = '<input type="hidden" name="business" value="' . htmlentities(PAYPAL_EMAIL) . '">';
			$image = '<input src="' . WEB_ROOT . 'images/btn_xpressCheckout.gif" name="submit" alt="Make payments with PayPal - it\'s fast, free and secure!" type="image" style="border-style: hidden">';
		}

		print <<<HTML

				<form id="$id" action="$location" method="post">
					<input type="hidden" name="cmd" value="_cart">
					<input type="hidden" name="upload" value="1">
					$business
HTML;

		$count = 1;

		foreach ($this->itemIDs as $key => $itemID) {
			// <input type="hidden" name="item_name_{$count}" value="({$this->itemQuantities[$itemID]}) {$this->itemNames[$itemID]}">

			$out_itemID = htmlentities($itemID);
			$format = htmlentities($this->itemFormats[$itemID]);
			$item_name = htmlentities($this->itemNames[$itemID]);
			$item_qty = htmlentities($this->itemQuantities[$itemID]);
			$item_prices = htmlentities($this->itemPrices[$itemID]);

			print <<<ITEM
					<input type="hidden" name="item_name_{$count}" value="$item_name">
					<input type="hidden" name="item_number_{$count}" value="$out_itemID">
					<input type="hidden" name="quantity_{$count}" value="$item_qty">
					<input type="hidden" name="amount_{$count}" value="$item_prices">
ITEM;

			$itemID = htmlentities($this->itemFormats[$itemID]);
			print <<<HTML
					<span style="visibility: hidden;">
						<input type="hidden" name="on0_$count" value="Format">
						<select name="os0_$count">
							<option>$itemID</option>
						</select>
					</span>
HTML;

			$count++;
		}
		$root = WEB_ROOT;
		$url = BSDDS_COMPLETED_URL;
		$currency = htmlentities(BSDDS_CURRENCY);
		print <<<HTML
					<input type="hidden" name="notify_url" value="{$root}notify.php">
					<input type="hidden" name="return" value="$url">
					<input type="hidden" name="shipping_1" id="shippingpaypal" value="0.00">
					<input type="hidden" name="currency_code" value="$currency">
					<div align="right">
						$image
					</div>
				</form>
HTML;
	}

	function output_free_form() {
		# Generate a transfer and payer id since we're not going through PayPal.
		$remote_addr = filter_input(INPUT_SERVER, 'REMOTE_ADDR', FILTER_SANITIZE_URL);
		$txn_id = htmlentities(md5(time() . $remote_addr));
		$payer_id = $txn_id;

		$root = WEB_ROOT;
		$currency = BSDDS_CURRENCY;
		print <<<HTML
				<form action="{$root}notify.php" method="post">
					<input type="hidden" name="txn_id" value="{$txn_id}">
					<input type="hidden" name="payment_gross" value="0.00">
					<input type="hidden" name="mc_gross" value="0.00">
					<input type="hidden" name="payer_id" value="{$payer_id}">
					<input type="hidden" name="payment_status" value="Completed">
					<input type="hidden" name="mc_shipping" value="0.00">
					<input type="hidden" name="mc_handling" value="0.00">
					<input type="hidden" name="first_name" value="Anonymous">
					<input type="hidden" name="num_cart_items" value="1">
					<input type="hidden" name="mc_currency" value="$currency">
					<input type="hidden" name="txn_type" value="cart">
					<input type="hidden" name="payer_email" value="{$remote_addr}">
					<input type="hidden" name="receiver_email" value="">
					<input type="hidden" name="receiver_id" value="">
					<input type="hidden" name="payment_type" value="instant">
					<input type="hidden" name="last_name" value="Anonymous">
					<input type="hidden" name="payment_fee" value="0">
HTML;
		$count = 1;

		foreach ($this->itemIDs as $key => $itemID) {
			$out_itemID = htmlentities($itemID);
			$format = htmlentities($this->itemFormats[$itemID]);
			$item_name = htmlentities($this->itemNames[$itemID]);
			$item_qty = htmlentities($this->itemQuantities[$itemID]);

			print <<<HTML
					<input type="hidden" name="item_number{$count}" value="{$out_itemID}">
					<input type="hidden" name="option_name{$count}_1" value="Format">
					<input type="hidden" name="option_selection{$count}_1" value="{$format}">
					<input type="hidden" name="item_name{$count}" value="{$item_name}">
					<input type="hidden" name="quantity{$count}" value="{$item_qty}">
					<input type="hidden" name="mc_gross_{$count}" value="0.00">
					<input type="hidden" name="mc_handling{$count}" value="0.00">
					<input type="hidden" name="mc_shipping{$count}" value="0.00">
					<input type="hidden" name="tax{$count}" value="0.00">
HTML;
			$count++;
		}

		print <<<HTML
					<input type="submit" value="Download your files" style="font-size: 17px;">
				</form>
HTML;
	}
}

?>